import React, { Component } from 'react';
import './Signln.scss'
import axios from 'axios'

class Signln extends Component {
    constructor(props) {
        super(props);
        this.state = {
            account: '',
            adminPwd: ''
        }
    }
    changVal(e) {
        this.setState({
            [e.target.name]: e.target.value
        })
    }
    zhuceUser() {
        const params = new URLSearchParams()
        params.append("account", this.state.account)
        params.append("adminPwd", this.state.adminPwd)
        axios.post('http://api.aulence.com/store-manage/w2001/admin_sign-in.php', params).then(res => {
            console.log(res.data);
            if (res.data === "success") {
                axios.get(`http://api.aulence.com/store-manage/w2001/admin_nickname.php? account=${this.state.account}`).then(res => {
                    console.log(res);
                    let nicknames = res.data
                    this.props.switchLogin_state(false, false,nicknames)
                })
            } else {
                alert(res.data)
            }
        }).catch(res => {
            console.error(res.data);
        })
    }

    render() {
        const { account, adminPwd } = this.state
        return (
            <div className="Signln" >
                <div></div>
                <div>
                    <div>
                        <div className="username">
                            <input type="txte" placeholder="请输入用户名" value={account} name='account' onChange={this.changVal.bind(this)}></input>
                        </div>
                        <div className="username">
                            <input type="password" placeholder="请输入密码" value={adminPwd} name='adminPwd' onChange={this.changVal.bind(this)}></input>
                        </div>
                        <p>是不是还没有账号？那就<span onClick={() => {
                            this.props.switchLogin_state(false, true)
                        }}>注册</span>吧！</p>
                        <div className="username">
                            <button onClick={this.zhuceUser.bind(this)}>登录</button>
                        </div>
                    </div>
                </div>
            </div>
        );
    }
}

export default Signln;